/*
 * @Author: asushiye
 * @Date: 2020-07-03 08:51:09
 * @Commit: file content
 */
import { Injectable } from '@angular/core';
import { AfMessageService, gw_code_g } from '@core';
import { _HttpClient } from '@delon/theme';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class LangService {
  fileType = 'tablelist';

  constructor(
    private http: _HttpClient,
    private msg: AfMessageService
  ) {}

  public get httpClient(): _HttpClient {
    return this.http;
  }

  //获取表列表
  public getTableListUrl(sourceName: any, tableName: any) {
    return `${gw_code_g}/lang/table/list?dbSource=${sourceName}&tableName=${tableName}`;
  }

  // 查询数据源， 返回cascader格式
  public queryDbSourceCascader(): Observable<any> {
    return this.http.get(`${gw_code_g}/db/datasource/cascader`);
  }

  // DDl脚本
  public getTableDDLScript(sourceType: any, sourceName: any, sourceTable: any, namingRule: any, scriptDataType: any): Observable<any> {
    const param = {
      sourceType: sourceType,
      sourceName: sourceName,
      sourceTable: sourceTable,
      namingRule: namingRule,
      scriptDataType: scriptDataType
    };
    return this.http.get(`${gw_code_g}/lang/table/ddl/script`, param);
  }

  public getTableAllScript(
    sourceType: any,
    sourceName: any,
    sourceTable: any,
    namingRule: any,
    scriptDataType: any,
    flinkDbSource: any,
    colPrefix: any,
    colSuffix: any,
    tableAlias: any
  ): Observable<any> {
    const param = {
      sourceType: sourceType,
      sourceName: sourceName,
      sourceTable: sourceTable,
      namingRule: namingRule,
      scriptDataType: scriptDataType,
      flinkDbSource: flinkDbSource,
      colPrefix: colPrefix,
      colSuffix: colSuffix,
      tableAlias: tableAlias
    };
    return this.http.get(`${gw_code_g}/lang/table/all/script`, param);
  }

  public executeDllScript(
    sourceType: any,
    sourceName: any,
    sourceTable: any,
    namingRule: any,
    scriptDataType: any,
    targetDbSource: any,
    forced: Boolean
  ): Observable<any> {
    const param = {
      sourceType: sourceType,
      sourceName: sourceName,
      sourceTable: sourceTable,
      namingRule: namingRule,
      scriptDataType: scriptDataType,
      targetDbSource: targetDbSource,
      forced: forced
    };
    return this.http.get(`${gw_code_g}/lang/table/execute/dll/script`, param);
  }

  // 查询flink sql
  public getTableFlinkDDLSql(
    sourceType: any,
    sourceName: any,
    sourceTable: any,
    namingRule: any,
    scriptDataType: any,
    flinkDbSource: any
  ): Observable<any> {
    const param = {
      sourceType: sourceType,
      sourceName: sourceName,
      sourceTable: sourceTable,
      namingRule: namingRule,
      scriptDataType: scriptDataType,
      flinkDbSource: flinkDbSource
    };
    return this.http.get(`${gw_code_g}/lang/table/flink/ddl/sql`, param);
  }

  // 查询表相关脚本
  public getTableDMLScript(sourceType: any, sourceName: any, sourceTable: any, namingRule: any, scriptDataType: any): Observable<any> {
    const param = {
      sourceType: sourceType,
      sourceName: sourceName,
      sourceTable: sourceTable,
      namingRule: namingRule,
      scriptDataType: scriptDataType
    };

    return this.http.get(`${gw_code_g}/lang/table/dml/script`, param);
  }

  // 表信息， 包含字段列表， 约束列表 String packageName, String moduleName, String author

  public getJpaCodeList(
    sourceType: any,
    sourceName: any,
    sourceTable: any,
    namingRule: any,
    packageName: any,
    moduleName: any,
    author: any
  ): Observable<any> {
    const param = {
      sourceType: sourceType,
      sourceName: sourceName,
      sourceTable: sourceTable,
      namingRule: namingRule,
      packageName: packageName,
      moduleName: moduleName,
      author: author
    };
    return this.http.get(`${gw_code_g}/lang/jpa/code/list`, param);
  }

  public downloadJpaCodeZipUrl(
    sourceType: any,
    sourceName: any,
    sourceTable: any,
    namingRule: any,
    packageName: any,
    moduleName: any,
    author: any
  ): string {
    return `${gw_code_g}/lang/jpa/code/zip?sourceType=${sourceType}&sourceName=${sourceName}&sourceTable=${sourceTable}&namingRule=${namingRule}&packageName=${packageName}&moduleName=${moduleName}&author=${author}`;
  }

  public getTableInfo(dbSource: any, tableName: any, namingRule: any): Observable<any> {
    const param = {
      dbSource: dbSource,
      tableName: tableName,
      namingRule: namingRule
    };
    return this.http.get(`${gw_code_g}/lang/table/info`, param);
  }

  isEmpty(value: any): boolean {
    if (value === null || value == undefined || value === '') {
      return true;
    }
    return false;
  }
}
